#ifndef QYAM3DVIEW_H
#define QYAM3DVIEW_H


#include <QtOpenGL>
#include <QTime>
#include <QTimer>
#include <QElapsedTimer>
#include <yam3dscene.h>
#include <core/ref.h>

typedef yam3d::Yam3DScene*(*SceneCreateFuncType)();

class QYam3DView : public QGLWidget
{
public:
    Q_OBJECT
 //   Q_DECLARE_PRIVATE(QGLWidget)
public:
    QYam3DView(QWidget *parent = 0, QGLWidget *shareWidget = 0);
   /* explicit QYam3DView(QWidget* parent=0,
                       const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
    explicit QYam3DView(QGLContext *context, QWidget* parent=0,
                       const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
    explicit QYam3DView(const QGLFormat& format, QWidget* parent=0,*/
   /*                    const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
#ifdef QT3_SUPPORT
    QT3_SUPPORT_CONSTRUCTOR QYam3DView(QWidget* parent, const char* name,
                                    const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
    QT3_SUPPORT_CONSTRUCTOR QYam3DView(QGLContext *context, QWidget* parent, const char* name,
                                    const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
    QT3_SUPPORT_CONSTRUCTOR QYam3DView(const QGLFormat& format, QWidget* parent, const char* name,
                                    const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
#endif*/
    virtual ~QYam3DView();

  /*  void qglColor(const QColor& c) const;
    void qglClearColor(const QColor& c) const;

    bool isValid() const;
    bool isSharing() const;

    // ### Qt 5: return bools
    void makeCurrent();
    void doneCurrent();

    bool doubleBuffer() const;
    void swapBuffers();

    QGLFormat format() const;
    void setFormat(const QGLFormat& format);

    const QGLContext* context() const;
    void setContext(QGLContext* context, const QGLContext* shareContext = 0,
                    bool deleteOldContext = true);

    QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
    QImage grabFrameBuffer(bool withAlpha = false);

    void makeOverlayCurrent();
    const QGLContext* overlayContext() const;

    static QImage convertToGLFormat(const QImage& img);

    void setMouseTracking(bool enable);

    const QGLColormap & colormap() const;
    void  setColormap(const QGLColormap & map);

    void renderText(int x, int y, const QString & str,
                     const QFont & fnt = QFont(), int listBase = 2000);
    void renderText(double x, double y, double z, const QString & str,
                     const QFont & fnt = QFont(), int listBase = 2000);
    QPaintEngine *paintEngine() const;

    GLuint bindTexture(const QImage &image, GLenum target, GLint format,
                       QGLContext::BindOptions options);
    GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
                       QGLContext::BindOptions options);

    GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
                       GLint format = GL_RGBA);
    GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
                       GLint format = GL_RGBA);

    GLuint bindTexture(const QString &fileName);

    void deleteTexture(GLuint tx_id);

    void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
    void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);

#ifdef Q_MAC_COMPAT_GL_FUNCTIONS
    GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
                       QMacCompatGLint format = GL_RGBA);
    GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
                       QMacCompatGLint format = GL_RGBA);
    GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
                       QGLContext::BindOptions);
    GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
                       QGLContext::BindOptions);

    void deleteTexture(QMacCompatGLuint tx_id);

    void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
    void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
#endif

public Q_SLOTS:
    virtual void updateGL();
    virtual void updateOverlayGL();
*/
protected:
//    bool event(QEvent *);
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();

    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);

    virtual void keyPressEvent ( QKeyEvent * event );
    virtual void keyReleaseEvent ( QKeyEvent * event );

    //virtual void initializeOverlayGL();
   // virtual void resizeOverlayGL(int w, int h);
  //  virtual void paintOverlayGL();

  //  void setAutoBufferSwap(bool on);
  //  bool autoBufferSwap() const;

    //void paintEvent(QPaintEvent*);
   // void resizeEvent(QResizeEvent*);

  //  virtual void glInit();
 //   virtual void glDraw();
 //   int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove

private:
    Q_DISABLE_COPY(QYam3DView)

public:
    void setSceneCreateFunc(SceneCreateFuncType pfCreateScene);
  //  QSize minimumSizeHint() const;
  //  QSize sizeHint() const;

    yam3d::Yam3DScene* getScene() const
    {
        return m_scene.ptr();
    }

    void setDesiredFrameTime(int desiredFrameTime)
    {
        m_desiredFrameTime = desiredFrameTime;
    }

/*
protected:
    virtual void initializeGL();
    virtual void paintGL();
    virtual void resizeGL(int width, int height);

signals:
*/
public slots:
   void update();

private:
  /*QYam3DView(QWidget* parent=0,
              const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
 */ // QYam3DView(const QYam3DView&);
  // QYam3DView& operator=(const QYam3DView&);

   void init();

   SceneCreateFuncType m_pfCreateScene;
   yam3d::core::Ref<yam3d::Yam3DScene>  m_scene;
   bool                m_initRequested;
   QTimer m_timer;
   QElapsedTimer m_clock;
   QElapsedTimer m_Fpsclock;
   int m_frameCount;
   int m_desiredFrameTime;

};

#endif // QYAM3DVIEW_H
